package com.example.libraryapp.notice;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.example.libraryapp.R;
import com.example.libraryapp.util.Validator;

/**
 * 公告详情
 * 
 */
public class NoticeDetailActivity extends FragmentActivity {
	private LinearLayout llContent;
	private TextView tvTitle;
	private Context context;
	private List<NoticeDetailInfo> noticeDetailList;
	private String did;
	private String title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_notice_detail);
		init();
		findViews();
		setListeners();
		showContent();
	}

	/**
	 * 初始化
	 */
	private void init() {
		this.context = NoticeDetailActivity.this;
		did = getIntent().getStringExtra("did");
		title = getIntent().getStringExtra("title");
		noticeDetailList = NoticeDetailService.getNoticeDetail(context,
				Integer.parseInt(did));

	}

	/**
	 * 找到控件
	 */
	private void findViews() {
		llContent = (LinearLayout) this.findViewById(R.id.ll_content);
		tvTitle = (TextView) this.findViewById(R.id.tv_title);
	}

	/**
	 * 设置监听
	 */
	private void setListeners() {
	}

	/**
	 * 显示内容
	 * 
	 */
	private void showContent() {
		tvTitle.setText("		" + title);
		llContent.removeAllViews();
		for (int i = 0; i < noticeDetailList.size(); i++) {
			NoticeDetailInfo noticeDetailInfo = noticeDetailList.get(i);
			if (noticeDetailInfo.getImageId() != -1) {
				ImageView iv = new ImageView(context);
				iv.setBackgroundResource(noticeDetailInfo.getImageId());
				LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(
						LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
				tvParams.leftMargin = 10;
				tvParams.topMargin = 10;
				tvParams.rightMargin = 10;
				tvParams.bottomMargin = 10;
				llContent.addView(iv, tvParams);
			}
			if (Validator.isEffective(noticeDetailInfo.getContent())) {
				TextView tvContent = new TextView(context);
				tvContent.setTextSize(18);
				tvContent.setTextColor(Color.BLACK);
				tvContent.setText("		" + noticeDetailInfo.getContent());
				LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(
						LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
				tvParams.leftMargin = 10;
				tvParams.topMargin = 10;
				tvParams.rightMargin = 10;
				tvParams.bottomMargin = 10;
				llContent.addView(tvContent, tvParams);
			}
		}
	}
}
